package co.fitstart.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.support.Page;

import co.fitstart.admin.dao.MovementDao;
import co.fitstart.admin.entity.search.MovementSearch;
import co.fitstart.admin.service.MovementService;
import co.fitstart.entity.courseware.movement.Movement;


/**
 * 
 * @author Wy
 *
 */

@Service("movementService")
public class MovementServiceImpl extends DataServiceImpl<Movement> implements MovementService{
	
	@Dynamic
    private  MovementDao movementDao;
	
	@Value("#{cloud['cloud.prod']}")
    private String cloudPath;
	
	/* (non-Javadoc)
	 * @see co.fitstart.admin.service.MovementService#list(com.demo2do.core.support.Page)
	 */
	public List<Movement> list(Page page) {
	    List<Movement> result = movementDao.list(page.getBeginIndex(), page.getMaxResultRecords());
	    
	    page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
	}
	
	/*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ActionLibraryService#search(co.fitstart.admin.entity.search.MovementSearch, com.demo2do.core.support.Page)
     */

	@SuppressWarnings("unchecked")
	public List<Movement> search(MovementSearch movementSearch, Page page) {
		
        List<Movement> result = genericDaoSupport.searchForList(movementSearch.buildSentenceTemplate(), movementSearch.buildParameters(), page.getBeginIndex(), page.getMaxResultRecords());
        
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
	}
	
	/*
	 * (non-Javadoc)
	 * @see co.fitstart.admin.service.MovementService#editVideo(co.fitstart.entity.courseware.movement.Movement, java.lang.String)
	 */
	public void editVideo(Movement movement, String video){
	    String videoLink = cloudPath + "/" + video;
        genericDaoSupport.update(movement.editVideoLink(videoLink));
	}

	/*
	 * (non-Javadoc)
	 * @see co.fitstart.admin.service.MovementService#editMovement(java.lang.Integer, java.lang.Integer, java.lang.String)
	 */
	public void editMovement(Integer groupAmount, Integer movementNumber, String maxWeight,Long movementID,String time) {
		
		Movement movement = movementDao.load(movementID);
		
		movement.addMovementNumber(groupAmount, movementNumber, maxWeight,time);	
		
		genericDaoSupport.update(movement);
		
	}

	
		

}
